﻿using Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;


namespace AppAdmin.Controllers
{
    public class HomeController : BaseController
    {
        [HttpGet]
        public ActionResult Default()
        {
            return View();
        }
        [HttpGet]
        public ActionResult About()
        {
            return View();
        }
        public JsonResult GetIndexInfo()
        {
            Model1Container db = new Model1Container();
            IndexReport peopleViews =   db.Database.SqlQuery<IndexReport>(@" select( select count(1) from UserComment where CheckStatus=0) as totalComment,
  (select count(1) from dbo.[Order] where OrderCreateTime>CONVERT(datetime,CONVERT(varchar(12) , getdate(), 103 ) ,103) and Status=1) as TotalCurrentDayOrder
  ,(select count(1) from dbo.[Order] where OrderCreateTime> convert(datetime,  convert(nvarchar(6),getdate(),112) +'01'))  as TotalCurrentMonthOrder").FirstOrDefault();
            return Json(JsonConvert.SerializeObject(peopleViews), JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetIndexInfoCrrent()
        {
            Model1Container db = new Model1Container();
           var peopleViews = db.Database.SqlQuery<IndexCurrent>(@"  select top 8 bb.Nickname,bb.Tel,aa.appointmentTime as gettime from  [dbo].[UserAppointment] aa
  left join UserInfo bb on aa.UserID=bb.Openid 
  where aa.appointmentTime>CONVERT(datetime,CONVERT(varchar(12) , getdate(), 103 ) ,103)  order by aa.appointmentTime desc

    select top 8 bb.Nickname,bb.Tel,aa.OrderCreateTime as gettime from  [dbo].[Order] aa
  inner join UserInfo bb on aa.UserID=bb.Openid 
  where aa.OrderCreateTime>CONVERT(datetime,CONVERT(varchar(12) , getdate(), 103 ) ,103) and aa.[Status]=1  order by aa.OrderCreateTime desc

      select top 8 bb.Nickname,bb.Tel,aa.CommentDate as gettime from  [dbo].[UserComment] aa
  inner join UserInfo bb on aa.UserID=bb.Openid 
    order by aa.CommentDate desc").ToList();
            return Json(JsonConvert.SerializeObject(peopleViews), JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetPerMonthNO()
        {
            Model1Container db = new Model1Container();
            var peopleViews = db.Database.SqlQuery<PerMonthCountModel>(@"       select (select count(1)from [Order] where  OrderCreateTime> (CONVERT(varchar,year(getdate())) +'-01-01') and OrderCreateTime<(CONVERT(varchar,year(getdate())) +'-02-01') and status=1) as month1  ,
      (select count(1)from [Order] where  OrderCreateTime> (CONVERT(varchar,year(getdate())) +'-02-01') and OrderCreateTime<(CONVERT(varchar,year(getdate())) +'-03-01') and status=1)as month2 ,
      (select count(1)from [Order] where  OrderCreateTime> (CONVERT(varchar,year(getdate())) +'-03-01') and OrderCreateTime<(CONVERT(varchar,year(getdate())) +'-04-01') and status=1)as month3  ,
      (select count(1)from [Order] where  OrderCreateTime> (CONVERT(varchar,year(getdate())) +'-04-01') and OrderCreateTime<(CONVERT(varchar,year(getdate())) +'-05-01') and status=1)as month4  ,
      (select count(1)from [Order] where  OrderCreateTime> (CONVERT(varchar,year(getdate())) +'-05-01') and OrderCreateTime<(CONVERT(varchar,year(getdate())) +'-06-01')and status=1)as month5  ,
      (select count(1)from [Order] where  OrderCreateTime> (CONVERT(varchar,year(getdate())) +'-06-01') and OrderCreateTime<(CONVERT(varchar,year(getdate())) +'-07-01')and status=1)as month6  ,
      (select count(1)from [Order] where  OrderCreateTime> (CONVERT(varchar,year(getdate())) +'-07-01') and OrderCreateTime<(CONVERT(varchar,year(getdate())) +'-08-01')and status=1)as month7  ,
      (select count(1)from [Order] where  OrderCreateTime> (CONVERT(varchar,year(getdate())) +'-08-01') and OrderCreateTime<(CONVERT(varchar,year(getdate())) +'-09-01')and status=1)as month8  ,
      (select count(1)from [Order] where  OrderCreateTime> (CONVERT(varchar,year(getdate())) +'-09-01') and OrderCreateTime<(CONVERT(varchar,year(getdate())) +'-10-01')and status=1)as month9  ,
      (select count(1)from [Order] where  OrderCreateTime> (CONVERT(varchar,year(getdate())) +'-10-01') and OrderCreateTime<(CONVERT(varchar,year(getdate())) +'-11-01')and status=1)as month10  ,
      (select count(1)from [Order] where  OrderCreateTime> (CONVERT(varchar,year(getdate())) +'-11-01') and OrderCreateTime<(CONVERT(varchar,year(getdate())) +'-12-01')and status=1)as month11 ,
      (select count(1)from [Order] where  OrderCreateTime> (CONVERT(varchar,year(getdate())) +'-12-01') and OrderCreateTime<(CONVERT(varchar,year(getdate())+1) +'-01-01')and status=1) as month12  ").ToList().First();
            int[] ints = new int[] { peopleViews.month1, peopleViews.month2, peopleViews.month3, peopleViews.month4, peopleViews.month5, peopleViews.month6, peopleViews.month7, peopleViews.month8, peopleViews.month9, peopleViews.month10, peopleViews.month11, peopleViews.month12};
            return Json(JsonConvert.SerializeObject(new { result = ints }), JsonRequestBehavior.AllowGet);
        }
    }
}
public class IndexReport
{
    public int totalComment { get;set;} 
     public int TotalCurrentDayOrder { get;set;} 
     public int TotalCurrentMonthOrder { get;set;} 
}
public class IndexCurrent
{
    public string Nickname { get; set; }
    public string Tel { get; set; }
    public DateTime appointmentTime { get; set; }
}
public class PerMonthCountModel
{
   public int  month1{ get; set; }
   public int month2 { get; set; }
   public int month3 { get; set; }
   public int month4 { get; set; }
   public int month5 { get; set; }
   public int month6 { get; set; }
   public int month7 { get; set; }
   public int month8 { get; set; }
   public int month9 { get; set; }
   public int month10 { get; set; }
   public int month11 { get; set; }
   public int month12 { get; set; }
       }